<?php
/**
 * Created by PhpStorm.
 * User: xinfeng
 * Date: 2017/8/23
 * Time: 11:30
 */

$ds = DIRECTORY_SEPARATOR;
$dir = realpath(dirname(__FILE__) . $ds .'..');
include_once($dir.'/models/UseRedis.php');
include_once($dir.'/models/UseMySql.php');

class Chat {

	/**
	 * 存储聊天消息
	 * @param array $info
	 */
	public function callSay($info = array()) {

		$redis = new UseRedis();
		$mysql = new UseMysql();
		$userSend = $info['userSend'];
		$userReceived = $info['userReceived'];
		$message = $info['message'];
		$messageType = $info['messageType'];
		$date = date('Y-m-d H:i:s');

		$data = array(
			'message'   => $message,
			'type'      => $messageType,
			'date'      => $date,
			'userSend'  => $userSend,
			'userReceived' => $userReceived
		);
		$mysql->setChatInfo($data);

		$data = array(
			'db'    => 0,
			'key'   => 'chatListNeedReceived:'. $userReceived,
			'value' => $userSend,
			'score' => 1
		);
		$redis->addSSetScore($data);
		//维护聊天列表
		$arr1 = array('db' => 0, 'key' => 'chatList:'.$userSend, 'value' => $userReceived, 'date' => time());
		$arr2 = array('db' => 0, 'key' => 'chatList:'.$userReceived, 'value' => $userSend,  'date' => time());
		$redis->setSSet($arr1);
		$redis->setSSet($arr2);
	}

	/**
	 * 获取聊天列表
	 * @param array $info
	 *         userid string
	 * @return array
	 */
	public function callGetChatList($info = array()) {
		//获取聊天列表
		$redis = new UseRedis();
		$mysql = new UseMysql();
		$data = array(
			'db' => 0,
			'key' => 'chatList:'. $info['userid'],
			'page'=> 1,
			'pageCount' => 100,
		);
		$res = $redis->getSSet($data);
		$chatList = array();
		$userList = array();
		if(!empty($res)) {
			foreach ($res as $key => $val) {
				$chatList[$key] = array('userid'=>$key, 'date' => $val, 'isNew'=> 0);
				$userList[] = $key;
			}

			//获取待接收聊天消息数目
			$data = array(
				'db' => 0,
				'key' => 'chatListNeedReceived:'. $info['userid'],
				'page'=> 1,
				'pageCount' => 100,
			);
			$res = $redis->getSSet($data);
			foreach ($res as $key => $val) {
				$chatList[$key]['user']  = $key;
				$chatList[$key]['name']  = $key;
				$chatList[$key]['len']   = $val;
				$chatList[$key]['isNew'] = 1;
			}

			//获取聊天消息 最近的一个
			$data = array(
				'user' => $info['userid'],
				'userList' => $userList,
			);
			$res = $mysql->getChatInfoOne($data);

			if ($res['error'] == 0 && !empty($res['data'])) {
				foreach ($res['data'] as $key => $val) {
					$val['date'] = $this->dealTime(intval($val['date']/1000));
					$chatList[$key]['value'] = $val;
				}
			}
		}

		$chatListRes = array();
		foreach ($chatList as $key => $val) {
			$chatListRes[] = $val;
		}
		return $chatListRes;

	}

	public function callGetChatInfo($info = array()) {

		$chatInfo = array();
		$mysql = new UseMysql();
		$data = array(
			'user1' => $info['userid'],
			'user2' => $info['userid2'],
			'page'=> (int)$info['page'],
			'pageCount' => 30,
		);
		$res = $mysql->getChatInfoAll($data);
		if ($res['error'] == 0 && !empty($res['data'])) {
			foreach ($res['data'] as $key => $val) {
				$val['date'] = date('Y-m-d H:i:s', intval($val['date']/1000));
				$chatInfo[$key] = $val;
			}
		}
		return $chatInfo;
	}

	/**
	 * 用户点击查看后
	 * @param array $info
	 */
	public function callViewed($info = array()) {

		$redis = new UseRedis();
		$data = array(
			'db' => 0,
			'key' => 'chatListNeedReceived:'. $info['user1'],
			'value' => $info['user2'],
		);
		$redis->delSSetByValue($data);
	}

	public function dealTime($time) {
		if (date('Ymd', $time) == date('Ymd')) {
			$time = date('H:i', $time);
		} else if (date('Y', $time) == date('Y')) {
			$time = date('m/d', $time);
		} else {
			$time = date('Y/m/d', $time);
		}
		return $time;
	}
}

//$call = new Chat();
//
//$data = array(
//	'userid'     => '10000000',
//	'userid2'     => '10000001',
//	'page'         => 1,
//	'pageCount'    => 30,
//);
//
//$a = $call->callGetChatInfo($data);
//var_dump($a);
////
//for ($i = 0; $i < 1000; $i++) {
//	$tem = (int)(rand(0, 5));
//	$temp = (int)(rand(0, 5));
//	$data = array(
//		'userSend' => '1000000'.$tem,
//		'userReceived' => '1000000'.$temp,
//		'message'     => '张三说他看到李四了311',
//		'messageType' => 1,
//		'data' => date('Y-m-d H:i:s'),
//	);
////	var_dump($tem, $temp);
//	$a = $call->callSay($data);
//}

